using Colorful.Apps.Models;
using Colorful.Database;
using Colorful.Image;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Colorful.Apps
{
    [Route("app")]
    public class AppController : API.BaseAPI
    {
        #region 临时
        //[Route("addapps112311")]
        //public IActionResult AddApps()
        //{
        //    using (var db = DbHelper.GetDb())
        //    {
        //        var app = new App()
        //        {
        //            Name = "会员管理系统",
        //            Icon = "users",
        //            ByOrder = db.Apps.GetId(),
        //            Url = "/ms/index"
        //        };
        //        db.Apps.Add(app);
        //        return this.GetTextResult($"共添加 {db.Apps.Count()} 个应用！");
        //    }
        //}
        #endregion

        #region 绑定
        [Route("bind")]
        public IActionResult Login(string mobile, string name, string uid)
        {
            try
            {
                if (string.IsNullOrEmpty(uid))
                    return this.GetResult(false, "用户标识不能为空！");
                using (var db = DbHelper.GetDb())
                {
                    var member = db.Members.FirstOrDefault(a => a.WeChatUserId == uid);
                    if (member == null)
                    {
                        member = new Member()
                        {
                            WeChatUserId = uid,
                            NumberId = db.Members.GetId(),
                            Status = Colorful.Models.AuditingStatus.Waiting,
                            Mobile = mobile,
                            Name = name,
                            AddDate = DateTime.Now,
                            LastModify = DateTime.Now
                        };
                        member.ByOrder = member.NumberId;
                        db.Members.Add(member);
                    }
                    var apps = GetApps(db, member);
                    return this.GetResult(true, new { Apps = apps, Member = new { member.Id, member.Status, member.Name, member.Photo } });
                }
            }
            catch (Exception ex)
            {
                return this.GetResult(ex);
            }
        }
        #endregion

        #region 获取App列表
        [Route("list")]
        public IActionResult GetApps(string uid)
        {
            using (var db = DbHelper.GetDb())
            {
                if (this.AppUser == null)
                    return this.GetResult(false);
                var member = db.Members.FirstOrDefault(a => a.Id == this.AppUser.Id);
                if (member == null)
                    return this.GetResult(false);
                var apps = GetApps(db, member);
                return this.GetResult(true, new { Apps = apps, Member = new { member.Id, member.Status, member.Name, member.Photo } });
            }
        }
        #endregion

        #region 检查是否登陆
        [Route("check/login")]
        public IActionResult CheckLogin()
        {
            return this.GetResult(this.User.Identity.IsAuthenticated);
        }
        #endregion

        private List<App> GetApps(DbContext db, Member member)
        {
            if (member.Apps == null)
                member.Apps = new List<int>();
            var apps = db.Apps.OrderBy(a => a.ByOrder).ToList();
            if (member.LoginId == "admin")
                return apps;
            return apps.Where(a => member.Apps.Contains(a.Id)).ToList();
            //apps.ForEach(item =>
            //{
            //    item.Owned = member.LoginId == "admin" || member.Apps.Any(a => a == item.Id);
            //    //item.Icon = CMS.Core.CMSHelper.GetIcon(item.Icon);
            //});
            //return apps;
        }
    }
}
